//
// ********************************************************************
// * License and Disclaimer                                           *
// *                                                                  *
// * The  Geant4 software  is  copyright of the Copyright Holders  of *
// * the Geant4 Collaboration.  It is provided  under  the terms  and *
// * conditions of the Geant4 Software License,  included in the file *
// * LICENSE and available at  http://cern.ch/geant4/license .  These *
// * include a list of copyright holders.                             *
// *                                                                  *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work  make  any representation or  warranty, express or implied, *
// * regarding  this  software system or assume any liability for its *
// * use.  Please see the license in the file  LICENSE  and URL above *
// * for the full disclaimer and the limitation of liability.         *
// *                                                                  *
// * This  code  implementation is the result of  the  scientific and *
// * technical work of the GEANT4 collaboration.                      *
// * By using,  copying,  modifying or  distributing the software (or *
// * any work based  on the software)  you  agree  to acknowledge its *
// * use  in  resulting  scientific  publications,  and indicate your *
// * acceptance of all terms of the Geant4 Software license.          *
// ********************************************************************
//
//
/// \file B1DetectorConstruction.cc
/// \brief Implementation of the B1DetectorConstruction class

#include "B1DetectorConstruction.hh"

#include "G4RunManager.hh"
#include "G4NistManager.hh"
#include "G4Box.hh"
#include "G4Cons.hh"
#include "G4Orb.hh"
#include "G4Sphere.hh"
#include "G4Trd.hh"
#include "G4LogicalVolume.hh"
#include "G4PVPlacement.hh"
#include "G4SystemOfUnits.hh"

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

B1DetectorConstruction::B1DetectorConstruction()
: G4VUserDetectorConstruction(),
  fScoringVolume(0)
{ }

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

B1DetectorConstruction::~B1DetectorConstruction()
{ }

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

G4VPhysicalVolume* B1DetectorConstruction::Construct()
{  
  // Get nist material manager
  G4NistManager* nist = G4NistManager::Instance();
  
  G4bool checkOverlaps = true;
  G4Material* env_mat = nist->FindOrBuildMaterial("G4_Galactic");
  G4double testspace_scale = 30.0*m;
  G4double target_scale = 30.0*cm;
  G4double shield_scale = 2.*mm;
  G4double Ta_Weight = 0.0;
  G4double Al_1_scale = shield_scale*(1.-Ta_Weight)*0.9;
  G4double Al_2_scale = shield_scale*(1.-Ta_Weight)*0.1;
  G4double Ta_scale = shield_scale*Ta_Weight;

  G4Box* Envelope_box = new G4Box("Envelope", testspace_scale, target_scale, target_scale);
  G4LogicalVolume* Envelope_log = new G4LogicalVolume(Envelope_box, env_mat, "Envelope");
  G4VPhysicalVolume* Envelope_phys = new G4PVPlacement(0, G4ThreeVector(), Envelope_log, "Envelope", 0, false, 0, checkOverlaps);

  G4Material* people_mat = nist->FindOrBuildMaterial("G4_TISSUE_SOFT_ICRU-4");
  G4Material* electric_mat = nist->FindOrBuildMaterial("G4_Si");
  G4Orb* target_orb = new G4Orb("target_orb", target_scale*0.5);

  G4LogicalVolume* target_log = new G4LogicalVolume(target_orb, electric_mat, "target");
  G4VPhysicalVolume* target_phys = new G4PVPlacement(0, G4ThreeVector(0, 0, 0), target_log, "target", Envelope_log, false, 0, checkOverlaps);
  
  if (shield_scale > 0.) {
    if (1 - Ta_Weight > 0.) {
      G4Material* Al_mat = nist->FindOrBuildMaterial("G4_Al");
      G4Box* Al_box_1 = new G4Box("Al_1", Al_1_scale, target_scale, target_scale);
      G4LogicalVolume* Al_log_1 = new G4LogicalVolume(Al_box_1, Al_mat, "Al_1");
      G4VPhysicalVolume* Al_phys_1 = new G4PVPlacement(0, G4ThreeVector(0.5*(testspace_scale+shield_scale-Al_1_scale), 0, 0), Al_log_1, "Al_1", Envelope_log, false, 0, checkOverlaps);
      
      G4Box* Al_box_2 = new G4Box("Al_2", Al_2_scale, target_scale, target_scale);
      G4LogicalVolume* Al_log_2 = new G4LogicalVolume(Al_box_2, Al_mat, "Al_2");
      G4VPhysicalVolume* Al_phys_2 = new G4PVPlacement(0, G4ThreeVector(0.5*(testspace_scale-shield_scale+Al_2_scale), 0, 0), Al_log_2, "Al_2", Envelope_log, false, 0, checkOverlaps);}

    if (Ta_Weight > 0.) {
      G4Material* Ta_mat = nist->FindOrBuildMaterial("G4_Ta");
      G4Box* Ta_box = new G4Box("Ta", Ta_scale, target_scale, target_scale);
      G4LogicalVolume* Ta_log = new G4LogicalVolume(Ta_box, Ta_mat, "Ta");
      G4VPhysicalVolume* Ta_phys = new G4PVPlacement(0, G4ThreeVector(0.5*(testspace_scale+shield_scale-Ta_scale)-Al_1_scale, 0, 0), Ta_log, "Ta", Envelope_log, false, 0, checkOverlaps);}}
      
  // Set Shape2 as scoring volume
  //
  fScoringVolume = target_log;

  //
  //always return the physical World
  //
  return Envelope_phys;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
